package com.example.answer_staff.api;

import java.util.HashMap;

import android.content.Context;
import android.util.Log;

import com.gaosai.staff.domain.Pager;
import com.gaosai.staff.utils.HttpClientUtils;
import com.gaosai.staff.utils.SPUtil;
import com.gaosai.staff.utils.ServicesHolder;
import com.loopj.android.http.AsyncHttpResponseHandler;

/**
 * api纺一管理类
 * @author willis
 *
 */
public class ApiManager {

	/**私有静态变量*/
	private static ApiManager apiManager ;
	public   Pager pager=new Pager();

	/**构造方法私有化*/
	private ApiManager(){} ;

	/**提供给外部取得单例的方法  懒汉式+线程安全的单例**/
	public static ApiManager getInstance(){

		/**可以先判断实例是否存在，若不存在再加锁，加上锁以后，同一时刻只有一个线程可以同时访问
		 * 下面的代码中，对实例是否为空我们进行了两次判断，为什么呢？
          因为当实例apiManager==null时，两个线程同时调用上面的代码，第一重条件都符合可以进入。
          此时由于lock机制，只能一个线程进入，另一个线程等候。这里如果没有第二重是否为null条件判断，
          第一个线程执行实例化对象退出后，第二个线程进入会再一次实例化对象。这样就没有达到我们单实例的目的。
		 * */
		if(null == apiManager){
			synchronized (ApiManager.class) {
				if(null == apiManager){
					apiManager = new ApiManager() ;
				}
			}
		}
		return apiManager ;
	}

	/**登陆 */

	public void login(Context context,String userName,String password,String udid,AsyncHttpResponseHandler handler){
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("userName", userName) ;
		params.put("password", password) ;
		params.put("udid", udid) ;

		HttpClientUtils.getInstance().post(
				ServicesHolder.api(ServicesHolder.USER_LOGIN),null, params,
				context, handler);

	}

	/**激活 */

	public void active(Context context,String code,String userName,String password,String udid,AsyncHttpResponseHandler handler){

		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("staffId", code) ;
		params.put("password", password) ;
		params.put("udid", udid) ;

		HttpClientUtils.getInstance().post(
				ServicesHolder.api(ServicesHolder.USER_REGIST),null, params,
				context, handler);
	}
	/**
	 * admin_show
	 * @param context
	 * @param handler
	 */
	public void adminshow(Context context,String page, String pageSize,String keyWord,String tagNames,AsyncHttpResponseHandler handler){

		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("pager.pagerNo", page) ;
		params.put("pager.pagerSize", pageSize) ;
		params.put("keyWord", keyWord) ;
		params.put("tagNames", tagNames) ;
		HttpClientUtils.getInstance().post(
				ServicesHolder.api(ServicesHolder.SHOW_ACTIVIEIES),SPUtil.getValue(context, SPUtil.USERID), params,
				context, handler);
	}
	/**
	 * show_news
	 * */

	public void showNews(Context context,int page,String keyword,String tagNames,AsyncHttpResponseHandler handler){

		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("pager.pageNo", page+"") ;
		params.put("keyword", keyword) ;
		params.put("tagNames", tagNames) ;
		HttpClientUtils.getInstance().post(
				ServicesHolder.api(ServicesHolder.SHOW_NEWS),SPUtil.getValue(context, SPUtil.USERID), params,
				context, handler);
		System.out.println("pagersize是"+pager.getPageSize());
		System.out.println("pagerNo是"+pager.getPageNo());
	}



	public void regist(Context context, String sb,
			String activity_id, AsyncHttpResponseHandler asyncHttpResponseHandler) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("app.enroll_info", sb) ;
		System.out.println("ApiMAnager的sb是"+sb.toString());
		Log.e("Apimmmmmmanager", sb.toString());
		params.put("activityId", activity_id) ;

		HttpClientUtils.getInstance().post(
				ServicesHolder.api(ServicesHolder.SHOW_JOINACTIVITY),null, params,
				context, asyncHttpResponseHandler);
	}
	public void changeDeviceQRCode(Context context,String barCode,
			String uniqueId, AsyncHttpResponseHandler asyncHttpResponseHandler) {

		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("code", barCode) ;
		params.put("udid", uniqueId) ;
		System.out.println("ApiMAnager的barcode是"+barCode);
		Log.e("Apimmmmmmanager", barCode);

		HttpClientUtils.getInstance().post(
				ServicesHolder.api(ServicesHolder.ACTIVITE_QRCODE),null, params,
				context, asyncHttpResponseHandler);
	}

	public void changePasword(Context context,
			String userName, String oldPassword, String newPassword,
			AsyncHttpResponseHandler asyncHttpResponseHandler) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("userName", userName) ;
		params.put("oldPassword", oldPassword) ;
		params.put("password", newPassword) ;
		Log.e("password............", params.toString());

		HttpClientUtils.getInstance().post(
				ServicesHolder.api(ServicesHolder.CHANGE_PASSWORD),null, params,
				context, asyncHttpResponseHandler);
	}

	public void quizshow(Context context,String page, String pageSize,String keyWord,String tagNames,AsyncHttpResponseHandler handler) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("pager.pagerNo", page) ;
		params.put("pager.pagerSize", pageSize) ;
		params.put("keyWord", keyWord) ;
		params.put("tagNames", tagNames) ;
		HttpClientUtils.getInstance().post(
				ServicesHolder.api(ServicesHolder.SHOW_QUIZ),SPUtil.getValue(context, SPUtil.USERID), params,
				context, handler);




	}

	public void Answersubmit(Context context, String quizId,
			String question_answer_str, AsyncHttpResponseHandler handler) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("quizId", quizId) ;
		params.put("question_answer_str", question_answer_str) ;
		HttpClientUtils.getInstance().post(
				ServicesHolder.api(ServicesHolder.SUBMIT_QUESTION),SPUtil.getValue(context, SPUtil.USERID), params,
				context, handler);
		
		
		
	}
}
